package com.sunq.studio.resource.controller;

import com.sunq.studio.resource.domain.TbPicResource;
import com.sunq.studio.resource.service.ResourceService;
import com.sunq.studio.sys.domain.SysResourceContent;
import com.sunq.studio.utils.JsonToOtherUtil;
import com.sunq.studio.utils.OtherPojo;
import com.sunq.studio.utils.ResultObjVO;
import com.sunq.studio.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.MessageFormat;
import java.util.List;

/**
 * Created by sunq on 2019/7/10.
 */
@RestController
@RequestMapping("/resource")
public class PicResourceController {

    Logger logger= LoggerFactory.getLogger(PicResourceController.class);
    @Autowired
    ResourceService resourceService;


    @PostMapping("/delResource")
    public ResultObjVO delResource(@RequestBody String data){

        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new TbPicResource() );
        TbPicResource tbPicResource= (TbPicResource)ob.getObj();
        if(tbPicResource.getId()==null||tbPicResource.getId()==0){
            logger.info(MessageFormat.format("图片资源{0}没有id",tbPicResource.getPicUrl()));

        }else {
            resourceService.delPicResource(tbPicResource.getId());
        }
        return ResultUtil.success("ok");

    }

    @PostMapping("/getResouceByClassify")
    public ResultObjVO getResouceByClassify(@RequestBody String data) {
        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new SysResourceContent());
        SysResourceContent sysResource = (SysResourceContent) ob.getObj();

       List<SysResourceContent> list= resourceService.getResoucesByClassify( Integer.parseInt(sysResource.getClassfiy()) );

       return ResultUtil.success(list);

    }

}
